//
//  CZNavigationController.swift
//  WeiBo
//
//  Created by Apple on 16/9/13.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

class CZNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置代理
        self.interactivePopGestureRecognizer?.delegate = self
    }
    //  重写push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //  添加完左侧按钮后，边缘手势不能使用了
        
        //  判断当前控制器里面的个数是 表示第二个控制器，显示第一个控制的表情
        if viewControllers.count == 1 {
            //  隐藏底部tabbar
            viewController.hidesBottomBarWhenPushed = true
            //  获取根视图控制器的标题
            if let title = viewControllers.first!.title {
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }
            
        } else {
            if viewControllers.count != 0 {
                //  不等于0表示第三个控制器，显示返回
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
                
            }
        }
        
        super.pushViewController(viewController, animated: true)
        
    }
    
    // MARK:    -点击事件
    @objc private func backAction() {
        self.popViewController(animated: true)
    }
    
    // MARK:    --UIGestureRecognizerDelegate
    //  是否开始接受执行的触摸的手势事件
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //  根视图控制器不让其开启边缘收拾
        if viewControllers.count == 1 {
            return false
        }
        return true
    }

}
